clear all
set more off, perm
local mydir "`1'"
global Tables 	`mydir'/TS/Tables
global Work   	`mydir'/TS/DataWork
global Raw    	`mydir'/TS/DataRaw
global WRDS  	`mydir'/WRDS
/* ********************************************************************* */
use $Work/cs02_tot, clear
keep datadate datem ActualE ActualS

tsset datem

generate ActualE1=F12.ActualE
generate ActualE2=F12.ActualE1
generate ActualE3=F12.ActualE2
generate ActualE4=F12.ActualE3
generate ActualE5=F12.ActualE4

generate fpedats1=datem+12
generate fpedats2=fpedats1+12
generate fpedats3=fpedats2+12
generate fpedats4=fpedats3+12
generate fpedats5=fpedats4+12

format fpedats* %tm

* assume it takes 4 months for data to be in the information set 
generate ActualE0=L4.ActualE
generate Sale_L0T=L4.ActualS
generate Sale_L1T=L12.Sale_L0T
generate Sale_L2T=L12.Sale_L1T
generate Sale_L3T=L12.Sale_L2T
generate Sale_L4T=L12.Sale_L3T

order datadate datem ActualE0 ActualE1-ActualE5 fpedats1-fpedats5 Sale_* 
keep if year(datadate)>=1975

keep datadate datem Sale_* ActualE0 ActualE1-ActualE5 fpedats1-fpedats5

tempfile Compustat
save "`Compustat'", replace
/* ********************************************************************* */
use $Work/cs03, clear
generate fpedats1=datem+12
format   fpedats1 %tm
drop datadate

merge 1:1 fpedats1 using "`Compustat'"
sort fpedats1
order _merge statpers datadate fpedats1
drop if _merge==2
drop _merge
/* ********************************************************************* */
save $Work/cs04, replace
/* ********************************************************************* */